<template>
    <el-tree :data="data" :props="defaultProps" :show-checkbox="showCheckbox"
             :default-expand-all="expandAll"
             :node-key="nodeKey"
             @check-change="checkChange"
             :check-strictly="checkStrictly"
             ref="commonTree"
             :default-checked-keys="checkedKeys" class="commonTree" @node-click="handleNodeClick"></el-tree>
</template>

<script>
    export default {
        data() {
            return {
                currentSelectedId:''
            }
        },
        watch: {
        },
        props: {
            data: {
                type: Array,
                default: function () {
                    return [];
                }
            },
            defaultProps: {
                type: Object,
                default: () => {
                    return {
                        children: 'children',
                        label: 'label',
                        isLeaf: 'leaf'
                    }
                }
            },
            showCheckbox: {
                type: Boolean,
                default: false
            },
            checkedKeys: {
                type: Array,
                default: function () {
                    return [];
                }
            },
            expandAll:{
                type: Boolean,
                default:false
            },
            nodeKey:{
                type:String,
                default:'id'
            },
            checkStrictly:{
                type: Boolean,
                default:false
            }
        },
        methods: {
            handleNodeClick(data,node) {
                if(this.currentSelectedId !== data.id) {
                    this.currentSelectedId = data.id;
                    this.$emit("node-click", node)
                }
            },

            setCheckedKeys(keys){
                this.$refs.commonTree.setCheckedKeys(keys);
            },

            checkChange(row,isChecked,childChecked){
//                console.log(row)
//                console.log(isChecked)
//                console.log(childChecked)
                this.$emit('check-change',row,isChecked,childChecked)

            },

            getCheckedNodes(){
                return this.$refs.commonTree.getCheckedNodes();
            },

            getHalfCheckedNodes(){
                return this.$refs.commonTree.getHalfCheckedNodes();
            }
        }
    }
</script>
